// 指令
import Vue from 'vue'

/**
 * 使用案例
    v-burying-point
    data-visitDataType="goods"
    data-visitEventType="clickContact"
    :data-supplierId="goodDetail.supplierId"
    :data-goodsId="goodDetail.id"
 */
export default Vue.directive('burying-point', {
    bind: function(el, binding, vnode) {
        el.addEventListener('click', (event) => {
            console.log(event, "点击事件", binding, el.dataset);
            const dataSet = el.dataset || {};
            const args = {
                userId: window.$nuxt.$store.state.user.userInfo.id || null, //访问者 id
                visitDataType: null, //访问数据类型
                // 数据类型是goods和supplier
                visitEventType: null, //访问事件类型
                /**
                 *  商品/店铺曝光 "viewpage";
                    商品详情 "viewpageGoods";
                    店铺详情 "viewpageSupplier";
                    点击电话 "clickPhone"
                    点击电子邮件 "clickEmail";
                    点击网站 "clickWebsite";
                    点击站内信 "clickContact";
                    点击发起会话 "clickChatNow";
                    邮件详情 "detailMail";
                    发送站内信 "sendMail";
                    站内信回信 "sendReplyMail";
                    点击发送会话 "sendChat";
                */
                visitPlatform: window.$nuxt.$tools.isMobile()?'移动端':'PC', //访问平台
                supplierId: null, //访问的供应商 id
                goodsId: null, //访问的商品 id
                ip: null, //访问者的 ip
                page: window.$nuxt.$route.fullPath //访问的页面
            };
            const params = {};
            Object.keys(args).map((key) => {
                const lowerKey = key.toLowerCase();
                if(dataSet&&dataSet[lowerKey] || args[key]){
                    params[key] = dataSet[lowerKey] || args[key];
                }
            });
            window.$nuxt.$store.dispatch("postRecord", [params]);
        });
    },
    inserted: function(el) {
        // console.log(el, "inserted")
    },
    update: function() {
        
    },
    componentUpdated: function() {

    },
    unbind: function(el) {
        el.removeEventListener('click', (event) => {
            console.log(event, "移除点击事件");
        });
    }
})
